<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/1
 * Time: 15:43
 */

namespace App\Weixin\Controllers\Admin;

use App\Base\Controllers\Controller;
use App\FreeBird\Contracts\StoreInterface;
use App\Weixin\Contracts\UserInterface;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;


class UserController extends Controller
{
    /**
     * 微信用户接口
     * @return UserInterface
     */
    public function getUserInterface()
    {
        return app()->make(UserInterface::class);
    }

    /**
     * 获取用户商店列表
     * @return StoreInterface
     */
    public function getStoreInterface()
    {
        return app()->make(StoreInterface::class);
    }

    /**
     * 用户列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|mixed
     */
    public function index(Request $request)
    {
        if ($request->getMethod() == "POST") {
            $where = function ($query) use ($request) {
                $lid = $request->input("lid");
                if (!empty($lid)) {
                    $query->where("lid", $lid);
                }
                $p1=$request->input("p1");
                if(!empty($p1)){
                    $query->where("p1", $p1);
                }
                $p2=$request->input("p2");
                if(!empty($p2)){
                    $query->where("p2", $p2);
                }
                $mobile=$request->input("mobile");
                if(!empty($mobile)){
                    $query->where("mobile", "like", '%' . $mobile . '%');
                }
                $truename=$request->input("truename");
                if(!empty($truename)){
                    $query->where("truename", "like", '%' . $truename . '%');
                }
                $start_date=$request->input("start_date");
                if(!empty($start_date)){
                    $query->where("create_time", ">", strtotime($start_date));
                }

                $end_date=$request->input("end_date");
                if(!empty($end_date)){
                    $query->where("create_time", "<", strtotime($end_date." 23:59:59"));
                }
                //时间

            };
            $list = $this->getUserInterface()->getList($where);
            foreach ($list as $k => $v) {
                $list[$k]->p1user_name = get_wxusername_by_uid($v->p1);
                $list[$k]->p2user_name = get_wxusername_by_uid($v->p2);
            }
            return $list;
        }
        return view("weixin/user/index");
    }

    /**
     *冻结用户
     */
    public function freeze(Request $request)
    {


        $data = array();
        $data["status"] = $request->input("status", 0);
        $uid = $request->input("uid");
        if ($r = $this->getUserInterface()->changeStatus(["uid" => $uid], $data)) {
            $key = Cache::get('uid');
            Cache::forget($key);
            return $r;
        }
    }

    public function show(Request $request)
    {
        $uid = $request->input("uid");
        $info = $this->getUserInterface()->getUserInfoById($uid);
//        print_r($info->toArray());
//        exit;
        return view("weixin/user/show")->with("info", $info);
    }

    /**
     * 用户商城列表
     * @param Request $request
     */
    public function shop(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            $where = function ($query) use ($request) {
                $title = $request->input("title");
                if (!empty($title)) {
                    $query->where("title", "like", '%' . $title . '%');
                }
                $uid = $request->input("uid");
                $query->where("uid", $uid);
                $type = $request->input("type");
                if (!empty($type)) {
                    $query->where("type", $type);
                }
            };
            return $this->getStoreInterface()->getList($where);
        }
        $uid = $request->input("uid");
        return view("weixin/user/shop")->with("uid", $uid);
    }

    /**
     * 用户编辑
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        //获取信息
        if ($request->getMethod() == 'POST') {
            $uid = $request->input("uid");
            return $this->getUserInterface()->changeStatus(['uid' => $uid], $request->except("file"));
        }
        $uid = $request->input("uid");
        $info = $this->getUserInterface()->getUserInfoById($uid);
        return view("weixin/user/edit")->with("data", $info); //用户编辑
    }
}